package com.dingreading.cloud.admin.util;

import com.dingreading.cloud.admin.dto.CaScheduleLessonDto;
import com.dingreading.cloud.admin.dto.CaTeacherDto;
import com.dingreading.cloud.admin.dto.CmorderDto;
import com.dingreading.cloud.common.util.DateUtil;
import com.dingreading.cloud.common.util.DoubleUtil;
import com.google.common.collect.HashBasedTable;
import com.google.common.collect.Table;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;

import java.text.ParseException;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 排课工具
 */
public class ScheduleUtil {


    /**
     * 星期数据集合
     */
    public static final String[] WeekDays = {"周日", "周一", "周二", "周三", "周四", "周五", "周六"};

    public static final String[] studentTypeNames = {"活动会员", "补缺会员", "插班会员", "试听会员"};
    public static final String[] callStatusNames = {"已到", "迟到", "请假", "未到"};

    public static final String TIYAN_UID = "50439d23284947dc8fc98f8ea5d1318e";
    public static final String HAODU_UID = "8067fd50183f4df4b808acbf34942590";
    public static final String JINGDU_UID = "9ad8250840254982a34ab750ac8e2d87";
    public static final String WENXUESHE_UID = "b2a70a55d7164a3b915893dcf22cb148";
    public static final String SIDAMINGZHU_UID = "5e4310bb66ce4c50861fdacab9788516";
    public static final String DOUXUEHUI_UID = "04299d70ebdf48b9b7b2ad69476b6145";
    public static final String JT_UID = "9d64ef8376354ccdaabd17ef349c0ed8";
    public static final String WK_UID = "c4896108a4134e5e9737c0ddc11722c0";
    public static final String HBJD_UID = "2eff6ea4351d491d83a077f94c7155c8";
    public static final String HSYD_UID = "dcb7e7f54224491abb22569b1b399b61";


    /**
     * 计算正价会员人数费用
     * 体验不算费用，只算正价和赠送
     * 精读会按人数比计费，文学社和四大名著固定一人15
     */
    public static double lessonPayCharge(String productUid, Integer payNum) {
        double amount = 0;
        if (StringUtils.isNotBlank(productUid) && null != payNum && payNum > 0) {
            if (productUid.equals(JINGDU_UID)) {
                // 精读会
                if (payNum > 10) {
                    amount = payNum * 15;
                } else if (payNum > 5) {
                    amount = payNum * 12;
                } else {
                    amount = payNum * 10;
                }
            } else if (productUid.equals(WENXUESHE_UID) || productUid.equals(SIDAMINGZHU_UID) || productUid.equals(DOUXUEHUI_UID)
                    || productUid.equals(JT_UID) || productUid.equals(WK_UID)) {
                // 文学社/四大名著/兜学慧/JT/WL
                amount = payNum * 15;
            } else if (productUid.equals(HBJD_UID) || productUid.equals(HSYD_UID)) {
                // 绘本精读/绘说有道
                amount = payNum * 10;
            }
        }
        return amount;
    }

    /**
     * 计算赠送会员人数费用
     * 不分服务项目。赠送固定一人6元
     */
    public static double lessonGiftCharge(Integer giftNum) {
        double amount = 0;
        if (null != giftNum && giftNum > 0) {
            amount = giftNum * 6;
        }
        return amount;
    }

    /**
     * 计算赠送会员人数费用
     * 不分服务项目。赠送固定一人6元
     */
    public static double lessonExpAmount(List<CmorderDto> expOrderList) throws ParseException {
        double amount = 0;
        // 68元订单开始时间
        Date date68 = DateUtil.parseDate("2024-05-28");
        if (CollectionUtils.isNotEmpty(expOrderList)) {
            for (CmorderDto cmorderDto : expOrderList) {
                Double orderAmount = cmorderDto.getOrderAmount();
                Date orderDate = cmorderDto.getOrderDate();
                // 68元订单计费
                if (orderAmount != null && orderDate != null
                        && orderAmount.equals(68d) && orderDate.after(date68))
                    amount = DoubleUtil.sum(amount, 5);
            }
        }
        return amount;
    }

    /**
     * 为活动列表设置教师集合
     */
    public static void listSetTeachers(List<CaScheduleLessonDto> list, List<CaTeacherDto> teachers) {
        if (CollectionUtils.isNotEmpty(teachers)) {
            // 获取活动计划里的导读
            LinkedHashMap<String, List<CaTeacherDto>> scheduleTeacherMap = getScheduleTeacherMap(teachers);
            // 获取活动里的导读
            LinkedHashMap<String, List<CaTeacherDto>> lessonTeacherMap = getLessonTeacherMap(teachers);

            for (CaScheduleLessonDto lesson : list) {
                // 先获取活动的自定义主讲导读，如果没有则获取活动表的主讲导读
                List<CaTeacherDto> teacherList = lessonTeacherMap.get(lesson.getLessonUid());
                if (CollectionUtils.isEmpty(teacherList))
                    teacherList = scheduleTeacherMap.get(lesson.getScheduleUid());
                lesson.setTeacherList(teacherList);
            }
        }
    }

    /**
     * 获取活动计划里的导读
     */
    private static LinkedHashMap<String, List<CaTeacherDto>> getScheduleTeacherMap(List<CaTeacherDto> teachers) {
        return teachers.stream()
                .filter(teacher -> StringUtils.isBlank(teacher.getLessonUid()))
                .collect(Collectors.groupingBy(CaTeacherDto::getScheduleUid, LinkedHashMap::new, Collectors.toList()));
    }

    /**
     * 获取活动自定义的导读
     */
    private static LinkedHashMap<String, List<CaTeacherDto>> getLessonTeacherMap(List<CaTeacherDto> teachers) {
        return teachers.stream()
                .filter(teacher -> StringUtils.isNotBlank(teacher.getLessonUid()))
                .collect(Collectors.groupingBy(CaTeacherDto::getLessonUid, LinkedHashMap::new, Collectors.toList()));
    }

    /**
     * 会员累计课时差额
     */
    public static Map<String, Table<String, String, Integer>> getLessonLoseNum() {
        Map<String, Table<String, String, Integer>> map = new HashMap<>();

        Table<String, String, Integer> table1 = HashBasedTable.create();
        table1.put("杨紫琪", "精读会", 14);
        table1.put("杨雨嘉", "精读会", 11);
        table1.put("梁宥琪", "精读会", 5);
        table1.put("费欣悦", "精读会", 3);
        table1.put("尹立宇", "精读会", 4);
        map.put("张丽君", table1);

        Table<String, String, Integer> table2 = HashBasedTable.create();
        table2.put("黄睿晨", "精读会", 14);
        table2.put("汪米诺", "精读会", 15);
        table2.put("赵弈涵", "精读会", 16);
        table2.put("王清湉", "精读会", 21);
        table2.put("潘汀晨", "精读会", 15);
        table2.put("李星玥", "精读会", 15);
        table2.put("李杨轩聆", "精读会", 7);
        table2.put("轩辕坤阳", "精读会", 14);
        table2.put("孔木子", "精读会", 2);
        table2.put("章玥琳", "精读会", 15);
        table2.put("曹陈子", "精读会", 8);
        table2.put("苏坦", "精读会", 13);
        table2.put("陈末", "精读会", 20);
        map.put("刘彤彤", table2);

        Table<String, String, Integer> table3 = HashBasedTable.create();
        table3.put("王靖豪2", "精读会", 14);
        table3.put("汪歆蕊", "精读会", 23);
        table3.put("高宏旭", "精读会", 14);
        table3.put("蔡雨欣", "精读会", 12);
        table3.put("王嘉禾", "精读会", 5);
        table3.put("葛天皛", "精读会", 14);
        table3.put("陈骏熙", "精读会", 26);
        table3.put("许召辰", "精读会", 11);
        table3.put("张可馨", "精读会", 12);
        table3.put("杜玗希", "精读会", 15);
        table3.put("杜玗涵", "精读会", 18);
        table3.put("郭婉瑜", "精读会", 14);
        map.put("陶军燕", table3);

        Table<String, String, Integer> table4 = HashBasedTable.create();
        table4.put("王楷瑞", "精读会", 13);
        table4.put("王世航", "精读会", 12);
        table4.put("李骏希", "精读会", 3);
        table4.put("花梓晗", "精读会", 2);
        table4.put("唐一鸣", "精读会", 6);
        table4.put("蒋雨溪", "精读会", 6);
        table4.put("姚柏宇", "精读会", 4);
        table4.put("周雅馨", "精读会", 3);
        table4.put("刘夏昕", "精读会", 8);
        map.put("朱爱玲", table4);

        Table<String, String, Integer> table5 = HashBasedTable.create();
        table5.put("朱欣怡", "文学社", 10);
        table5.put("方康晴", "文学社", 18);
        table5.put("张砚博", "文学社", 17);
        map.put("宫佳会", table5);

        Table<String, String, Integer> table6 = HashBasedTable.create();
        table6.put("耿楚皓", "文学社", 5);
        table6.put("余一鸣", "文学社", 60);
        table6.put("刘子乐", "文学社", 60);
        map.put("任雨梅", table6);

        Table<String, String, Integer> table7 = HashBasedTable.create();
        table7.put("胡振涵", "文学社", 21);
        table7.put("王晗清", "文学社", 18);
        table7.put("王梓轩", "文学社", 18);
        table7.put("马岩", "文学社", 18);
        table7.put("杨瀚辰", "文学社", 19);
        table7.put("方雅琪", "文学社", 18);
        table7.put("庞国周", "文学社", 18);
        table7.put("尹世祺", "文学社", 19);
        table7.put("朱甜睿", "文学社", 15);
        table7.put("鲁星亮", "文学社", 16);
        table7.put("尹立宇", "文学社", 16);
        map.put("王婉", table7);

        Table<String, String, Integer> table8 = HashBasedTable.create();
        table8.put("张赵帅", "文学社", 14);
        table8.put("吕思宇", "文学社", 18);
        table8.put("易宸羽", "文学社", 34);
        table8.put("王子睿", "文学社", 18);
        table8.put("胡铭涵", "精读会", 16);
        table8.put("李明浩", "精读会", 19);
        table8.put("易宸羽", "精读会", 10);
        table8.put("赵茗扬", "文学社", 8);
        table8.put("刘洪潮", "文学社", 9);
        table8.put("张庭瑞", "文学社", 52);
        table8.put("刘彦清", "文学社", 45);
        map.put("李静", table8);

        Table<String, String, Integer> table9 = HashBasedTable.create();
        table9.put("张子坤", "精读会", 20);
        table9.put("徐永程", "精读会", 21);
        table9.put("许雅睿", "精读会", 23);
        table9.put("余一鸣", "精读会", 17);
        table9.put("程康薇", "精读会", 15);
        table9.put("朱梓涵", "精读会", 15);
        table9.put("汪煜桐", "精读会", 16);
        table9.put("魏雨航", "精读会", 22);
        table9.put("张修齐", "精读会", 9);
        table9.put("李聪聪", "精读会", 17);
        table9.put("孙牧宸", "文学社", 12);
        table9.put("刘钰浠", "精读会", 16);
        table9.put("熊皓扬", "精读会", 17);
        table9.put("张智逸", "精读会", 20);
        table9.put("吴涵蕊", "精读会", 15);
        table9.put("郑语彤", "精读会", 6);
        table9.put("何思阳", "精读会", 10);
        table9.put("汤烁兮", "精读会", 12);
        map.put("沈悦悦", table9);

        Table<String, String, Integer> table10 = HashBasedTable.create();
        table10.put("朱子伊", "精读会", 16);
        table10.put("余传皓", "精读会", 3);
        table10.put("汪柏杨", "精读会", 6);
        map.put("化云娜", table10);

        return map;
    }

}
